from User import *
import serial
import re

#########################
# Christmas Project '08 #
#   by Jeff Monroe      #
#                       #
#########################
# #'s specify RF commands
# /'s specify microcontroller commands

def cls():
  for i in range(15):
    print "\n"

def weather(zipcode):
  return zipcode

def toScreen(s):
  board.write(s)

def wordWrap(s):
  for i in range(20, 100, 20):
    if(len(s) > i and s[i - 1] != " " and s[i] != " "):
      j = i
      while(s[j] != " "):
        j = j - 1
      s = s[:j] + " " * ((i - 1) - j) + s[i:]
  return s
#  if(len(s) > 20):
#    if(s[19] != " " and s[20] != " "):
#      i = 20
#      while(s[i] != " "):
#        i = i - 1
#      s = s[:i] + " " * (19 - i) + s[i:]
#  elif(len(s) > 40):
#    if(s[39] != " " and s[40] != " "):
#      i = 40
#      while(s[i] != " "):
#        i = i - 1
#      s = s[:i] + " " * (19 - i) + s[i:]
#  elif(len(s) > 60):
#    if(s[59] != " " and s[60] != " "):
#      i = 60
#      while(s[i] != " "):
#        i = i - 1
#      s = s[:i] + " " * (19 - i) + s[i:]
#  elif(len(s) > 80):
#    if(s[79] != " " and s[80] != " "):
#      i = 80
#      while(s[i] != " "):
#        i = i - 1
#      s = s[:i] + " " * (19 - i) + s[i:]
#  return s

board = serial.Serial(port=6, baudrate=38400)
cls()
print "  ****************************************************  "
print " *                Christmas '08 Project               * "
print " *                         by                         * "
print " *                    Scott  Gauche                   * "
print " *                     Jeff Monroe                    * "
print " *                    Phil DeMonaco                   * "
print "  ****************************************************  "
print "\n\n"
name = raw_input("Username: ")
channel = raw_input("Channel: ")
client = user(name, channel)
cls()

#tell clients new user is logged on
#board.write("#new_user " + name)

while 1:
  prompt = raw_input(client.name + "> ")
  output = prompt
  #output[3] = client.name + "> " + " " * (18 - len(client.name))
  if(output[0][0] == "/"):
    toScreen(output[0])
  else:  
    toScreen(wordWrap(client.name + ">" + "".join(map(str,output)) + chr(0x0d)))
  #print "".join(map(str,output))
board.close()